package tech.xs.sys.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.stereotype.Service;
import tech.xs.common.sys.domain.bo.CurrLoginUserBo;
import tech.xs.common.domain.enums.YesNoEnum;
import tech.xs.common.framework.service.CrudServiceImpl;
import tech.xs.common.framework.util.AuthUtil;
import tech.xs.common.framework.util.BaseEntityUtil;
import tech.xs.common.util.FieldUtil;
import tech.xs.sys.dao.SysWebMenuDao;
import tech.xs.sys.dao.SysWebMenuShowDao;
import tech.xs.sys.domain.bo.web.menu.CurrUserMenuBo;
import tech.xs.sys.domain.bo.web.menu.GetWebRouteBo;
import tech.xs.sys.domain.entity.SysWebMenu;
import tech.xs.sys.domain.entity.SysWebMenuShow;
import tech.xs.sys.domain.enums.SysWebMenuShowType;
import tech.xs.sys.service.SysUserRoleService;
import tech.xs.sys.service.SysWebMenuShowService;

import javax.annotation.Resource;
import java.util.*;

@Service
public class SysWebMenuShowServiceImpl extends CrudServiceImpl<SysWebMenuShowDao, SysWebMenuShow> implements SysWebMenuShowService {

    @Resource
    private SysWebMenuShowDao sysWebMenuShowDao;
    @Resource
    private SysWebMenuDao sysWebMenuDao;
    @Resource
    private SysUserRoleService sysUserRoleService;

    @Override
    public GetWebRouteBo getCurrUserRoutes() throws Exception {
        // 查询登陆账号都可以看到的菜单
        List<SysWebMenuShow> sysWebMenuShows = sysWebMenuShowDao.selectList(Wrappers.<SysWebMenuShow>lambdaQuery()
                .eq(SysWebMenuShow::getShowType, SysWebMenuShowType.LOGIN));
        Set<String> menuCodes = new HashSet<>(FieldUtil.toSet(sysWebMenuShows, SysWebMenuShow::getWebMenuCode));

        // 查询当前登陆用户所属角色能看到的菜单
        CurrLoginUserBo currLoginUser = AuthUtil.getCurrLoginUser();
        String currUserName = currLoginUser.getUserName();
        Set<String> roleCodes = sysUserRoleService.getRoleCodesByUserName(currUserName);
        if (!roleCodes.isEmpty()) {
            sysWebMenuShows = sysWebMenuShowDao.selectList(Wrappers.<SysWebMenuShow>lambdaQuery()
                    .eq(SysWebMenuShow::getShowType, SysWebMenuShowType.ROLE)
                    .in(SysWebMenuShow::getRoleCode, roleCodes));
            menuCodes.addAll(FieldUtil.toSet(sysWebMenuShows, SysWebMenuShow::getWebMenuCode));
        }

        List<SysWebMenu> loginMenus = sysWebMenuDao.selectList(Wrappers.<SysWebMenu>lambdaQuery()
                .eq(SysWebMenu::getMenuStatus, YesNoEnum.YES)
                .in(SysWebMenu::getCode, menuCodes));
        List<SysWebMenu> menus = new ArrayList<>(loginMenus);
        menus = BaseEntityUtil.duplicateRemovalByIdToList(menus);

        GetWebRouteBo routeBo = new GetWebRouteBo();
        routeBo.setMenus(getCurrWebMenus(menus));
        return routeBo;
    }

    private List<CurrUserMenuBo> getCurrWebMenus(List<SysWebMenu> menus) {
        // 菜单实体类转换成前端数据
        List<CurrUserMenuBo> webMenus = new ArrayList<>();
        for (SysWebMenu menu : menus) {
            CurrUserMenuBo bo = new CurrUserMenuBo();
            bo.setCode(menu.getCode());
            bo.setFatherCode(menu.getFatherCode());
            bo.setShowName(menu.getShowName());
            bo.setOrderValue(menu.getOrderValue());
            bo.setRouterPath(menu.getPageUri());
            webMenus.add(bo);
        }
        webMenus.sort(Comparator.comparingInt(CurrUserMenuBo::getOrderValue));
        return webMenus;
    }

}
